import {
  isNumber,
  isPhone,
  isPercentage,
  isEmail,
  isMobile,
  isIdCode,
  isPositiveInteger,
  isPlateNumber,
  isMoney
} from '@/utils/validate'
import { isArray } from 'lodash'

const validNumber = (param, rule, value, callback) => {
  if (!value) {
    return callback()
  }

  if (!isNumber(value) && value) {
    callback(new Error('请输入正整数'))
  }

  if (value.toString().length < param.min) {
    callback(new Error(`最少输入${param.min}个字符`))
  }

  if (value.toString().length > param.max) {
    callback(new Error(`最多输入${param.max}个字符`))
  }
  callback()
}

const validPositiveInteger = (param, rule, value, callback) => {
  if (!value) {
    return callback()
  }
  if (!isPositiveInteger(value) && value) {
    callback(new Error('请输入正整数'))
  }

  if (value.toString().length < param.min) {
    callback(new Error(`最少输入${param.min}个字符`))
  }

  if (value.toString().length > param.max) {
    callback(new Error(`最多输入${param.max}个字符`))
  }
  callback()
}

const validDecimal = (param, rule, value, callback) => {
  if (!value) {
    return callback()
  }

  if (!isMoney(value) && value) {
    callback(new Error('请输入合法的数字，最多两位小数'))
  }
  if (value.toString().length < param.min) {
    callback(new Error(`最少输入${param.min}个字符`))
  }

  if (value.toString().length > param.max) {
    callback(new Error(`最多输入${param.max}个字符`))
  }
  callback()
}

const validPhone = (rule, value, callback) => {
  if (!isPhone(value)) {
    callback(new Error('请输入正确的电话号码格式'))
  }
  callback()
}
const validPlateNumber = (rule, value, callback) => {
  if (!isPlateNumber(value) && value !== '' && value !== undefined && value !== null) {
    callback(new Error('请输入正确的车牌号格式'))
  }
  callback()
}
const validIdCode = (rule, value, callback) => {
  if (!isIdCode(value)) {
    callback(new Error('请输入正确身份证格式'))
  }
  callback()
}
const validPercentage = (rule, value, callback) => {
  if (!isPercentage(value)) {
    callback(new Error('请输入正确的百分比'))
  }
  callback()
}
const validateEmail = (param, rule, value, callback) => {
  if (!param && value === '') {
    return callback()
  }

  if (!isEmail(value)) {
    return callback(new Error('请填写正确的邮箱地址'))
  }
  callback()
}
const validateMobile = (param, rule, value, callback) => {
  if (!param && value === '') {
    return callback()
  }
  if (!isMobile(value)) {
    return callback(new Error('请输入正确的手机号'))
  }
  callback()
}

const validPhoneAndMobile = (param, rule, value, callback) => {
  if (!param.val && !value) {
    callback()
  }
  if (!isMobile(value) && !isPhone(value)) {
    return callback(new Error('请输入正确的电话号'))
  }
  callback()
}

// const validSelect = (param, rule, value, callback) => {
//   value = value || ''
//   let length = value.split(',').length

//   if (param.type === 'max') {
//     if (length >= param.num) {
//       callback(new Error(`最多选择${param.num}个`))
//     }
//   }

//   if (param.type === 'min') {
//     if (length <= param.num) {
//       callback(new Error(`至少选择${param.num}个`))
//     }
//   }

//   callback()
// }
const validSelect = (param, rule, value, callback) => {
  value = isArray(value) ? value : []
  let length = value.length

  if (param.type === 'max') {
    if (length > param.num) {
      return callback(new Error(`最多选择${param.num}个`))
    }
  }

  if (param.type === 'min') {
    if (length < param.num) {
      return callback(new Error(`至少选择${param.num}个`))
    }
  }
  callback()
}
export default {
  methods: {
    required () {
      return [{
        required: true,
        message: '必填项不能为空',
        trigger: 'change'
      }]
    },
    idCodeRule (val = true) {
      return [{
        required: val,
        message: '必填项不能为空',
        trigger: 'change'
      },
      {
        validator: validIdCode,
        trigger: 'change'
      }
      ]
    },
    numberRule (val = true, min = 0, max = 20) {
      return [{
        required: val,
        message: '必填项不能为空',
        trigger: 'change'
      },
      {
        validator: validNumber.bind(this, {
          min,
          max
        }),
        trigger: 'change'
      }
      ]
    },
    phoneAndMobileRule (val = true) {
      return [{
        required: val,
        message: '必填项不能为空',
        trigger: 'change'
      },
      {
        validator: validPhoneAndMobile.bind(this, {
          val
        }),
        trigger: 'change'
      }
      ]
    },
    selectRule (type = 'max', num = 0) {
      return [{
        required: true,
        message: '必填项不能为空',
        trigger: 'change'
      },
      {
        validator: validSelect.bind(this, {
          type,
          num
        }),
        trigger: 'change'
      }
      ]
    },
    positiveIntegerRule (val = true, min = 0, max = 20) {
      return [{
        required: val,
        message: '必填项不能为空',
        trigger: 'change'
      },
      {
        validator: validPositiveInteger.bind(this, {
          min,
          max
        }),
        trigger: 'change'
      }
      ]
    },
    decimalRule (val = true, min = 0, max = 20) {
      return [{
        required: val,
        message: '必填项不能为空',
        trigger: 'change'
      },
      {
        validator: validDecimal.bind(this, {
          min,
          max
        }),
        trigger: 'change'
      }
      ]
    },
    stringRule (val = true, min = 0, max = 20) {
      if (val) {
        if (min < 1) {
          min = 1
        }
      }
      return [{
        required: val,
        message: '必填项不能为空',
        trigger: 'change'
      },
      {
        min,
        max,
        message: `请输入${min}到${max}个字符`,
        trigger: 'change'
      }
      ]
    },
    phoneRule (val = true) {
      return [{
        required: val,
        message: '必填项不能为空',
        trigger: 'change'
      },
      {
        validator: validPhone,
        trigger: 'change'
      }
      ]
    },
    plateNumberRule (val = true) {
      return [{
        required: val,
        message: '必填项不能为空',
        trigger: 'change'
      },
      {
        validator: validPlateNumber,
        trigger: 'change'
      }
      ]
    },
    percentageRule (val = true) {
      return [{
        required: val,
        message: '必填项不能为空',
        trigger: 'change'
      },
      {
        validator: validPercentage,
        trigger: 'change'
      }
      ]
    },
    emailRule (val = true) {
      return [{
        required: val,
        message: '必填项不能为空',
        trigger: 'change'
      },
      {
        validator: validateEmail.bind(this, val),
        trigger: 'change'
      }
      ]
    },
    mobileRule (val = true) {
      return [{
        required: val,
        message: '必填项不能为空',
        trigger: 'change'
      },
      {
        validator: validateMobile.bind(this, val),
        trigger: 'change'
      }
      ]
    },
    validatorRule (validator, val = true) {
      return [{
        required: val,
        message: '必填项不能为空',
        trigger: 'change'
      },
      {
        validator,
        trigger: 'change'
      }
      ]
    },
    // 电话/手机号校验，允许为空
    phoneAndMobileRuleCanNull (val) {
      return [{
        validator: validPhoneAndMobile.bind(this, {
          val
        }),
        trigger: 'change'
      }]
    }
  }
}
